package com.sdkj.controller.company;

import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sdkj.controller.base.BaseController;
import com.sdkj.entity.Page;
import com.sdkj.service.company.BoApplyService;
import com.sdkj.service.company.BoChatService;
import com.sdkj.util.AppUtil;
import com.sdkj.util.ObjectExcelView;
import com.sdkj.util.Const;
import com.sdkj.util.PageData;
import com.sdkj.util.Tools;
import com.sdkj.util.Jurisdiction;

/** 
 * 类名称：BoApplyController
 * 创建人：FH 
 * 创建时间：2017-05-23
 */
@Controller
@RequestMapping(value="/boapply")
public class BoApplyController extends BaseController {
	
	String menuUrl = "boapply/list.do"; //菜单地址(权限用)
	@Resource(name="boapplyService")
	private BoApplyService boapplyService;
	@Resource(name="bochatService")
	private BoChatService bochatService;
	/**
	 * 新增
	 */
	@RequestMapping(value="/save")
	public ModelAndView save() throws Exception{
		logBefore(logger, "新增BoApply");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "add")){return null;} //校验权限
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		pd.put("bo_id", this.get32UUID());	//主键
		//为申请添加系统时间
		Date date=new Date();
		pd.put("application_time", Tools.date2Str(date));
		pd.put("chat_status",1);
		 pd.put("use_id", getCurrentUser().getUSER_ID());
		boapplyService.save(pd);
		mv.addObject("msg","success");
		mv.setViewName("save_result");
		return mv;
	}
	/**
	 * 去修改页面
	 */
	@RequestMapping(value="/goEdit")
	public ModelAndView goEdit(){
		logBefore(logger, "去修改BoAdd页面");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		String a=pd.getString("b");
		try {
			pd = boapplyService.findById(pd);	//根据ID读取
			mv.addObject("a", a);
			mv.setViewName("company/boapply/boapply_editInfo");
			mv.addObject("msg", "edit");
			mv.addObject("pd", pd);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}						
		return mv;
	}	
	/**
	 * 删除
	 */
	@RequestMapping(value="/delete")
	public void delete(PrintWriter out){
		logBefore(logger, "删除BoApply");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "del")){return;} //校验权限
		PageData pd = new PageData();
		try{
			pd = this.getPageData();
			boapplyService.delete(pd);
			out.write("success");
			out.close();
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		
	}
	/**
	 * 分公司信息查询
	 */
	@RequestMapping(value="/boinfo")
	public ModelAndView boinfo(Page page){
		logBefore(logger,"查询分公司信息");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "edit")){return null;}
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd=this.getPageData();
		try {
			List<PageData>	varList = boapplyService.boinfo(page);
			mv.setViewName("company/boinfo/boinfo_list");
			mv.addObject("varList", varList);
			mv.addObject("pd", pd);
			mv.addObject(Const.SESSION_QX,this.getHC());	//按钮权限
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}
		return mv;
	}
	/**
	 * 注销申请管理
	 */
	@RequestMapping(value="/bologoff")
	public ModelAndView bologoff(Page page){
		logBefore(logger,"注销申请管理");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "edit")){return null;}
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd=this.getPageData();
		try {
			List<PageData>	varList = boapplyService.bologoff(page);
			mv.setViewName("company/boinfo/bologoff_list");
			mv.addObject("varList", varList);
			mv.addObject("pd", pd);
			mv.addObject(Const.SESSION_QX,this.getHC());	//按钮权限
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}
		return mv;
	}
	/**
	 * 提交注销申请
	 */
	@RequestMapping(value="/logoff")
	public void logoff(HttpServletResponse response){
		logBefore(logger,"提交注销申请");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "edit")){return;}
		PageData pd = new PageData();
		pd=this.getPageData();
		try {
			boapplyService.logoff(pd);
			System.out.println(pd);
			response.getWriter().print("success");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 	提交分公司信息
	 */
	@RequestMapping(value="/addBo")
	public ModelAndView addBo() throws Exception{
		logBefore(logger, "修改BoApply");
	if(!Jurisdiction.buttonJurisdiction(menuUrl, "edit")){return null;} //校验权限
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		pd.put("establish_time", Tools.date2Str(new Date()));
		 boapplyService.edit(pd);
		mv.addObject("msg","success");
		mv.setViewName("save_result");
		return mv;
	}
	
	/**
	 * 列表
	 */
	@RequestMapping(value="/list")
	public ModelAndView list(Page page){
		logBefore(logger, "列表BoApply");
	if(!Jurisdiction.buttonJurisdiction(menuUrl, "cha")){return null;} //校验权限
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		try{
			pd = this.getPageData();
			String b=pd.getString("b");
			String keyWord=pd.getString("keyWord");
			if(keyWord==null||"".equals(keyWord)){
				pd.put("keyWord", keyWord);
			}
			String applyDate=pd.getString("applyDate");
			if(applyDate==null||"".equals(applyDate)){
				 pd.put("applyDate", applyDate);
			}
			page.setPd(pd);
			List<PageData>	varList = boapplyService.list(page);	//列出BoApply列表
			mv.addObject("varList", varList);
			mv.addObject("pd", pd);
			mv.addObject(Const.SESSION_QX,this.getHC());	//按钮权限
	        if("1".equals(b)){
	           mv.setViewName("company/bond/boBond_Retreatlist");
	         }else if("2".equals(b)){
	        	 mv.setViewName("company/managementexpense/bome_Retreatlist");
	         }
	         else if("3".equals(b)){
	        	 mv.setViewName("company/applyofloan/boapplyofloan_list");
	         }
	        else{
			  mv.setViewName("company/boapply/boapply_list");
	        }
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return mv;
	}
	@RequestMapping(value="/editList")
	public ModelAndView editList(Page page){
		logBefore(logger, "列表BoApply");
	if(!Jurisdiction.buttonJurisdiction(menuUrl, "cha")){return null;} //校验权限
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		try{
			pd = this.getPageData();
			String keyWord=pd.getString("keyWord");
			if(keyWord==null||"".equals(keyWord)){
				pd.put("keyWord", keyWord);
			}
			String applyDate=pd.getString("applyDate");
			if(applyDate==null||"".equals(applyDate)){
				 pd.put("applyDate", applyDate);
			}
			page.setPd(pd);
			List<PageData>	varList = boapplyService.applyList(page);	//列出BoApply列表
			System.out.println(varList);
			mv.addObject("varList", varList);
			mv.addObject("pd", pd);
			mv.addObject(Const.SESSION_QX,this.getHC());	//按钮权限
	
			mv.setViewName("company/boapply/boapply_editList");
		
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return mv;
	}
	/**
	 * 去新增页面
	 */
	@RequestMapping(value="/goAdd")
	public ModelAndView goAdd(){
		logBefore(logger, "去新增BoApply页面");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			mv.setViewName("company/boapply/boapply_add");
			mv.addObject("msg", "save");
			mv.addObject("pd", pd);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}						
		return mv;
	}	
	
	/**
	 * 去添加分公司页面
	 */
	@RequestMapping(value="/goAddBo")
	public ModelAndView goAddBo(){
		logBefore(logger, "去添加分公司页面");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		PageData pd2 = new PageData();
		pd = this.getPageData();
		pd2=this.getPageData();
		try {
			pd = boapplyService.findById(pd);	//根据ID读取
			pd2=bochatService.findById(pd);
			mv.setViewName("company/boapply/bo_add");
			mv.addObject("msg", "addBo");
			mv.addObject("pd", pd);
			mv.addObject("pd2", pd2);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}						
		return mv;
	}	
     /**
      * 去详情页面
      */
	@RequestMapping(value="/goinfo")
	 public ModelAndView goInfo(){
		logBefore(logger,"去详情页面");
		ModelAndView mv=this.getModelAndView();
		PageData pd=new PageData();
		pd=this.getPageData();
		try {
			pd=boapplyService.findById(pd);
			mv.setViewName("company/boapply/bo_info");
			mv.addObject("pd", pd);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}
		return mv;
	}
	/**
	 * 去注销申请页面
	 */
	@RequestMapping(value="/logoff_apply")
	 public ModelAndView logoff_apply(){
		logBefore(logger,"去注销申请页面");
		ModelAndView mv=this.getModelAndView();
		PageData pd=new PageData();
		pd=this.getPageData();
		try {
			pd.put("bo_id", pd.getString("bo_id"));
			mv.addObject("pd", pd);
			mv.addObject("msg","logoffReason");
			mv.setViewName("company/boinfo/logoff_apply");
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}
		return mv;
	}
	/**
	 * 添加注销原因
	 */
	@RequestMapping(value="/logoffReason")
	 public ModelAndView logoffReason(){
		logBefore(logger,"添加注销原因");
		ModelAndView mv=this.getModelAndView();
		PageData pd=new PageData();
		pd=this.getPageData();
		try {
			boapplyService.logoffReason(pd);
			System.out.println(pd);
			mv.addObject("msg","success");
			mv.setViewName("save_result");
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}
		return mv;
	}
	/**
	 * 变更申请
	 */
	@RequestMapping(value="/updateApply")
	public ModelAndView updateApply(){
		logBefore(logger,"变更申请");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "cha")){return null;} 
		ModelAndView mv=this.getModelAndView();
		PageData pd=new PageData();
		pd=this.getPageData();
		try {
			boapplyService.updateApply(pd);
			mv.addObject("msg", "success");
			mv.setViewName("save_result");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 批量删除
	 */
	@RequestMapping(value="/deleteAll")
	@ResponseBody
	public Object deleteAll() {
		logBefore(logger, "批量删除BoApply");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "dell")){return null;} //校验权限
		PageData pd = new PageData();		
		Map<String,Object> map = new HashMap<String,Object>();
		try {
			pd = this.getPageData();
			List<PageData> pdList = new ArrayList<PageData>();
			String DATA_IDS = pd.getString("DATA_IDS");
			if(null != DATA_IDS && !"".equals(DATA_IDS)){
				String ArrayDATA_IDS[] = DATA_IDS.split(",");
				boapplyService.deleteAll(ArrayDATA_IDS);
				pd.put("msg", "ok");
			}else{
				pd.put("msg", "no");
			}
			pdList.add(pd);
			map.put("list", pdList);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		} finally {
			logAfter(logger);
		}
		return AppUtil.returnObject(pd, map);
	}
	
	/*
	 * 导出到excel
	 * @return
	 */
	@RequestMapping(value="/excel")
	public ModelAndView exportExcel(){
		logBefore(logger, "导出BoApply到excel");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "cha")){return null;}
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try{
			Map<String,Object> dataMap = new HashMap<String,Object>();
			List<String> titles = new ArrayList<String>();
			titles.add("");	//1
			titles.add("");	//2
			titles.add("");	//3
			titles.add("");	//4
			titles.add("");	//5
			titles.add("");	//6
			titles.add("");	//7
			titles.add("");	//8
			titles.add("");	//9
			titles.add("");	//10
			titles.add("");	//11
			titles.add("");	//12
			titles.add("");	//13
			titles.add("");	//14
			titles.add("");	//15
			titles.add("");	//16
			titles.add("");	//17
			titles.add("");	//18
			titles.add("");	//19
			dataMap.put("titles", titles);
			List<PageData> varOList = boapplyService.listAll(pd);
			List<PageData> varList = new ArrayList<PageData>();
			for(int i=0;i<varOList.size();i++){
				PageData vpd = new PageData();
				vpd.put("var1", varOList.get(i).getString("bo_id"));	//1
				vpd.put("var2", varOList.get(i).getString("bo_name"));	//2
				vpd.put("var3", varOList.get(i).getString("applicant"));	//3
				vpd.put("var4", varOList.get(i).getString("application_time"));	//4
				vpd.put("var5", varOList.get(i).getString("operating_margin"));	//5
				vpd.put("var6", varOList.get(i).getString("management_expense"));	//6
				vpd.put("var7", varOList.get(i).getString("registered_address"));	//7
				vpd.put("var8", varOList.get(i).getString("person"));	//8
				vpd.put("var9", varOList.get(i).getString("province"));	//9
				vpd.put("var10", varOList.get(i).getString("province_addr"));	//10
				vpd.put("var11", varOList.get(i).getString("territorial_city"));	//11
				vpd.put("var12", varOList.get(i).getString("creat_time"));	//12
				vpd.put("var13", varOList.get(i).getString("region"));	//13
				vpd.put("var14", varOList.get(i).getString("preparing_for_business"));	//14
				vpd.put("var15", varOList.get(i).getString("budget_management_fee"));	//15
				vpd.put("var16", varOList.get(i).getString("introducer"));	//16
				vpd.put("var17", varOList.get(i).getString("introducer_phone"));	//17
				vpd.put("var18", varOList.get(i).getString("relation_company"));	//18
				vpd.put("var19", varOList.get(i).getString("add_info_man"));	//19
				varList.add(vpd);
			}
			dataMap.put("varList", varList);
			ObjectExcelView erv = new ObjectExcelView();
			mv = new ModelAndView(erv,dataMap);
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return mv;
	}
	
	/* ===============================权限================================== */
	public Map<String, String> getHC(){
		Subject currentUser = SecurityUtils.getSubject();  //shiro管理的session
		Session session = currentUser.getSession();
		return (Map<String, String>)session.getAttribute(Const.SESSION_QX);
	}
	/* ===============================权限================================== */
	
	@InitBinder
	public void initBinder(WebDataBinder binder){
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(format,true));
	}
	
public static void main(String[] args) {
	double x=0;
	//System.out.println(x=(1+Math.random())*0x10000 | 0.0);
}
}
